﻿package core.ui.util
{
    import __AS3__.vec.*;
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.image.decoder.*;
    import core.image.encoder.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.utils.*;

    public class Brushes extends FloatBox
    {
        var ed:Tuyitu;
        var bp:BrushPad;
        var bm:Bitmap;
        public var shape:Brush;
        public var brushes:Vector.<Brush>;
        var scroller:Scrollbar;
        var holder:Sprite;
        var presets:Dropdown;
        var diameter:Slidebar;
        var hardness:Slidebar;
        var step:Slidebar;
        var cbi:BrushInfo;

        public function Brushes(param1:Tuyitu)
        {
            super(480, 355);
            this.ed = param1;
            this.visible = false;
            this.diameter = new Slidebar("rm_diameter", 1, 999, 100, 5, false, false, 28);
            this.diameter.addEventListener(SliderEvent.CHANGE, this.SetStepChange, false, 1, true);
            this.diameter.addEventListener(SliderEvent.CHANGE, this.Settled, false, 1, true);
            this.diameter.x = 16;
            this.diameter.y = 305;
            addChild(this.diameter);
            this.step = new Slidebar("rm_spacing", 1, 200, 100, 0, false, false, 28);
            this.step.addEventListener(SliderEvent.CHANGE, this.Settled, false, 1, true);
            this.step.x = 138;
            this.step.y = 305;
            addChild(this.step);
            this.hardness = new Slidebar("rm_hardness", 0, 100, 100, 100, false, false, 28);
            this.hardness.addEventListener(SliderEvent.CHANGE, this.SetStepChange, false, 1, true);
            this.hardness.addEventListener(SliderEvent.CHANGE, this.Settled, false, 1, true);
            this.hardness.x = 260;
            this.hardness.y = 305;
            addChild(this.hardness);
            var _loc_2:* = new Sprite();
            _loc_2.graphics.beginFill(0, 0.35);
            _loc_2.graphics.drawRect(0, 0, 368, 1);
            _loc_2.graphics.beginFill(16777215, 0.5);
            _loc_2.graphics.drawRect(0, 1, 368, 1);
            _loc_2.graphics.endFill();
            _loc_2.x = 4;
            _loc_2.y = 291;
            addChild(_loc_2);
            var _loc_3:* = new Sprite();
            _loc_3.graphics.beginFill(0, 0.35);
            _loc_3.graphics.drawRect(0, 0, 1, 336);
            _loc_3.graphics.beginFill(16777215, 0.5);
            _loc_3.graphics.drawRect(1, 0, 1, 336);
            _loc_3.graphics.endFill();
            _loc_3.x = 372;
            _loc_3.y = 9;
            addChild(_loc_3);
            this.holder = new Sprite();
            this.holder.addEventListener(MouseEvent.ROLL_OVER, this.HolderMouseOver, false, 1, true);
            this.holder.addEventListener(MouseEvent.ROLL_OUT, this.HolderMouseOut, false, 1, true);
            this.holder.x = 5;
            this.holder.y = 10;
            addChild(this.holder);
            this.scroller = new Scrollbar(this.holder, new Rectangle(0, 0, 350, 280));
            this.scroller.x = 356;
            this.scroller.y = 10;
            addChild(this.scroller);
            var _loc_4:* = new Button("rm_browse_brush_sets", 90);
            _loc_4.addEventListener(MouseEvent.CLICK, this.BrowseClick, false, 0, true);
            _loc_4.x = 380;
            _loc_4.y = 22;
            addChild(_loc_4);
            var _loc_5:* = new Button("rm_load_brush_set", 90);
            _loc_5.addEventListener(MouseEvent.CLICK, this.LoadClick, false, 0, true);
            _loc_5.x = 380;
            _loc_5.y = 60;
            addChild(_loc_5);
            var _loc_6:* = new Button("rm_save_brush_set", 90);
            _loc_6.addEventListener(MouseEvent.CLICK, this.SaveClick, false, 0, true);
            _loc_6.x = 380;
            _loc_6.y = 85;
            addChild(_loc_6);
            var _loc_7:* = new Button("rm_create_new_brush", 90);
            _loc_7.addEventListener(MouseEvent.CLICK, this.CreateClick, false, 0, true);
            _loc_7.x = 380;
            _loc_7.y = 125;
            addChild(_loc_7);
            var _loc_8:* = new Button("rm_remove_brush", 90);
            _loc_8.addEventListener(MouseEvent.CLICK, this.TrashClick, false, 0, true);
            _loc_8.x = 380;
            _loc_8.y = 150;
            addChild(_loc_8);
            var _loc_9:* = new Button("rm_clear", 90);
            _loc_9.addEventListener(MouseEvent.CLICK, this.Clear, false, 0, true);
            _loc_9.x = 380;
            _loc_9.y = 190;
            addChild(_loc_9);
            var _loc_10:* = new Button("rm_reset", 90);
            _loc_10.addEventListener(MouseEvent.CLICK, this.Reset, false, 0, true);
            _loc_10.x = 380;
            _loc_10.y = 215;
            addChild(_loc_10);
            this.Reset();
            return;
        }// end function

        public function Show(rm_remove_brush:BrushPad) : void
        {
            var _loc_2:BrushInfo = null;
            var _loc_3:int = 0;
            this.bp = rm_remove_brush;
            this.shape = rm_remove_brush.shape;
            this.visible = true;
            stage.addEventListener(MouseEvent.MOUSE_DOWN, this.Hide, true, 1, true);
            if (this.cbi != null)
            {
                this.cbi.Marking(false);
                this.cbi = null;
            }
            while (_loc_3 < this.holder.numChildren)
            {
                
                _loc_2 = this.holder.getChildAt(_loc_3) as BrushInfo;
                if (_loc_2.shape.ID == this.shape.ID)
                {
                    _loc_2.Marking(true);
                    this.cbi = _loc_2;
                    break;
                }
                _loc_3++;
            }
            this.step.Set(this.shape.step);
            if (this.shape.type != BrushType.Custom)
            {
                this.diameter.Set(this.shape.details.radius * 2);
                this.hardness.Set(this.shape.details.hardness * 100);
            }
            else
            {
                this.diameter.Set(this.shape.size);
                this.hardness.Set(100);
            }
            this.hardness.Disable(Boolean(this.shape.type == BrushType.Custom));
            return;
        }// end function

        public function Hide(event:MouseEvent = null) : void
        {
            if (!AppState.Modald && (event == null || !this.hitTestPoint(stage.mouseX, stage.mouseY)))
            {
                this.visible = false;
                stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.Hide, true);
            }
            return;
        }// end function

        private function SaveClick(... args) : void
        {
            args = new PBREncoder();
            var _loc_3:* = new FileReference();
            _loc_3.save(args.encode(this.brushes), "brushes.pbr");
            return;
        }// end function

        private function LoadClick(... args) : void
        {
            args = new activation;
            var file:*;
            var _args:* = args;
            file;
            var args:* = ;
            file = new FileReference();
            addEventListener(Event.SELECT, function ()
            {
                file.load();
                return;
            }// end function
            );
            addEventListener(Event.COMPLETE, function ()
            {
                DecodePBR(file.data);
                return;
            }// end function
            );
            browse(new Array(new FileFilter(U.RM("rm_brushes") + " (*.pbr)", "*.PBR;*.pbr;*.Pbr;")));
            return;
        }// end function

        private function DecodePBR(rm_remove_brush:ByteArray) : void
        {
            var _loc_2:* = new PBRDecoder();
            this.AddPack(_loc_2.decode(rm_remove_brush));
            return;
        }// end function

        private function Reset(... args) : void
        {
            this.PrepareStandardBrushes();
            this.RenderBrushes();
            return;
        }// end function

        private function Clear(... args) : void
        {
            this.brushes = new Vector.<Brush>;
            this.RenderBrushes();
            return;
        }// end function

        private function SetStepChange(... args) : void
        {
            this.step.Set(Math.round(this.diameter.value * 0.1 + this.diameter.value * 0.1 * (this.hardness.value * 0.01)));
            return;
        }// end function

        private function Settled(... args) : void
        {
            if (this.shape.type != BrushType.Custom)
            {
                this.shape.details.radius = this.diameter.value * 0.5;
                this.shape.details.hardness = this.hardness.value * 0.01;
                this.shape.details.step = this.step.value;
                this.shape = Misc.GenerateBrush(this.shape.details);
            }
            else
            {
                this.shape = Misc.ResizeBrush(this.shape, this.diameter.value, this.hardness.value, this.step.value);
            }
            this.bp.SetBrush(this.shape);
            return;
        }// end function

        public function Add(rm_remove_brush:Brush) : void
        {
            this.brushes.push(rm_remove_brush);
            this.RenderBrushes();
            return;
        }// end function

        public function AddPack(rm_remove_brush:Vector.<Brush>) : void
        {
            this.brushes = this.brushes.concat(rm_remove_brush);
            this.RenderBrushes();
            return;
        }// end function

        public function RenderBrushes() : void
        {
            var _loc_1:BrushInfo = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (this.cbi != null)
            {
                this.cbi.Marking(false);
                this.cbi = null;
            }
            while (this.holder.numChildren > 0)
            {
                
                this.holder.removeChildAt(0);
            }
            while (_loc_4 < this.brushes.length)
            {
                
                if (_loc_4 > 0 && _loc_4 % 10 == 0)
                {
                    _loc_3 = _loc_3 + 45;
                    _loc_2 = 0;
                }
                _loc_1 = new BrushInfo(this.brushes[_loc_4] as Brush);
                _loc_1.addEventListener(MouseEvent.MOUSE_DOWN, this.BrushMouseDown, false, 1, true);
                _loc_1.x = _loc_2;
                _loc_1.y = _loc_3;
                _loc_1.nr = _loc_4;
                this.holder.addChild(_loc_1);
                _loc_2 = _loc_2 + 35;
                if (this.shape != null && _loc_1.shape.ID == this.shape.ID)
                {
                    this.MarkBrush(_loc_1);
                }
                _loc_4++;
            }
            this.scroller.SetContentHeight(_loc_3 + 45);
            return;
        }// end function

        private function HolderMouseOver(event:MouseEvent) : void
        {
            this.ed.cursor.Set(ToolType.Click);
            return;
        }// end function

        private function HolderMouseOut(event:MouseEvent) : void
        {
            this.ed.cursor.Set(ToolType.None);
            return;
        }// end function

        private function BrushMouseDown(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as BrushInfo;
            if (!_loc_2.marked)
            {
                this.MarkBrush(_loc_2);
            }
            return;
        }// end function

        public function MarkBrush(rm_remove_brush:BrushInfo) : void
        {
            if (this.cbi)
            {
                this.cbi.Marking(false);
            }
            this.shape = rm_remove_brush.shape;
            this.step.Set(this.shape.step);
            if (this.shape.type != BrushType.Custom)
            {
                this.diameter.Set(this.shape.details.radius * 2);
                this.hardness.Set(this.shape.details.hardness * 100);
            }
            else
            {
                this.diameter.Set(this.shape.size);
                this.hardness.Set(100);
            }
            this.hardness.Disable(Boolean(this.shape.type == BrushType.Custom));
            rm_remove_brush.Marking(true);
            this.bp.SetBrush(rm_remove_brush.shape);
            this.cbi = rm_remove_brush;
            return;
        }// end function

        private function BrowseClick(event:MouseEvent) : void
        {
            new BrushBrowser(this.ed);
            return;
        }// end function

        private function CreateClick(event:MouseEvent) : void
        {
            new BrushTuyitu(this.ed);
            return;
        }// end function

        private function TrashClick(event:MouseEvent) : void
        {
            if (this.cbi)
            {
                this.brushes.splice(this.cbi.nr, 1);
                if (this.brushes.length > 0)
                {
                    var _loc_2:* = this.cbi;
                    _loc_2.nr = this.cbi.nr - 1;
                    if (--this.cbi.nr < 0)
                    {
                        this.cbi.nr = 1;
                    }
                    this.MarkBrush(this.holder.getChildAt(this.cbi.nr) as BrushInfo);
                }
                this.RenderBrushes();
            }
            return;
        }// end function

        public function PrepareStandardBrushes()
        {
            this.brushes = new Vector.<Brush>;
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 0.5, 2, 0.7, 1, 0, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 2.5, 2, 0.65, 1, 0, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 4.5, 2, 0.85, 1, 0, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 7.5, 2, 0.95, 1, 0, 1)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 10, 2, 0.95, 1, 0, 2)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 15, 2, 1, 1, 0, 5)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 25, 2, 1, 1, 0, 7)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 35, 2, 1, 1, 0, 12)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 50, 2, 1, 1, 0, 20)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 100, 2, 1, 1, 0, 40)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 4.5, 2, 0.5, 1, 0, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 7.5, 2, 0.5, 1, 0, 1)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 10, 2, 0.5, 1, 0, 2)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 12.5, 2, 0.5, 1, 0, 2)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 15, 2, 0.5, 1, 0, 3)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 25, 2, 0.5, 1, 0, 4)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 35, 2, 0.5, 1, 0, 7)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 50, 2, 0.5, 1, 0, 11)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 75, 2, 0.5, 1, 0, 15)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 100, 2, 0.5, 1, 0, 22)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 0.5, 2, 1, 1, 0, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 1.5, 2, 1, 1, 0, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 2.5, 2, 1, 1, 0, 1)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 3.5, 2, 1, 1, 0, 1)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 4.5, 2, 1, 1, 0, 2)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 7.5, 2, 1, 1, 0, 3)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 12.5, 2, 1, 1, 0, 6)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 15.5, 2, 1, 1, 0, 7)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 20.5, 2, 1, 1, 0, 10)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 30.5, 2, 1, 1, 0, 15)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Square, 45, 7, 0.7, 5, 0, 50)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Diamond, 45, 7, 0.7, 5, 0, 50)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Diamond, 30, 5, 0.95, 3.3, 9, 34)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 30, 5, 0.97, 3.3, 45, 34)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 42.5, 10, 0.97, 3.3, 0, 50)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 10, 2, 1, 5.2, 45, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Circle, 20, 2, 1, 7.5, 45, 0)));
            this.brushes.push(Misc.GenerateBrush(new ShapeDetails(BrushType.Diamond, 10, 5, 0)));
            return;
        }// end function

    }
}
